using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Super
{
	public class ExcelEditor : EditorWindow
	{

		public class ExcelFileOperate
		{
			public Action<string> action;
			public string btnText;
			public ExcelFileOperate(Action<string> action, string btnText)
			{
				this.action = action;
				this.btnText = btnText;
			}
		}

		public class ExcelAllOperate
		{
			public Action Oper;
			public string OperText;
			public ExcelAllOperate(Action allOper, string allOperText)
			{
				this.Oper = allOper;
				this.OperText = allOperText;
			}
		}

		string excelPath = @"";
		ExcelOperateType operType = ExcelOperateType.LoadData;
		bool showContent = false;
		Vector2 scrollPos = Vector2.zero;

		List<ExcelAllOperate> allOperates1 = new List<ExcelAllOperate>()
		{
			new ExcelAllOperate(EditorUtil.GenerateAllExcelStruct, "重新生成所有结构体"),
			new ExcelAllOperate(ExcelUtil.LoadAllExcel, "重新导入所有"),
		};

		List<ExcelFileOperate> operates1 = new List<ExcelFileOperate>()
		{
			new ExcelFileOperate(EditorUtil.GenerateExcelStruct, "重新生成"),
			new ExcelFileOperate(ExcelUtil.LoadExcel, "重新导入"),
			new ExcelFileOperate(EditorUtil.NewStructAndLoadExcel, "生成并导入")
		};
		List<ExcelFileOperate> operates2 = new List<ExcelFileOperate>()
		{
			new ExcelFileOperate(EditorUtil.RemoveExcelSO, "移除"),
		};

		List<ExcelFileOperate> operates3 = new List<ExcelFileOperate>()
		{
			new ExcelFileOperate(ExcelUtil.WriteExcel, "写入Excel文件"),
		};

		

		[MenuItem("Framework/ExcelEditor")]
		public static void Init()
		{
			ExcelEditor exWindow = GetWindow<ExcelEditor>();
			exWindow.minSize = new Vector2(700, 350);
			exWindow.maxSize = new Vector2(1000, 500);
			exWindow.Show();
		}

		public void OnGUI()
		{
			#region
			EditorGUILayout.LabelField("Excel数据与Asset资源处理");
			operType = (ExcelOperateType)GUILayout.Toolbar((int)operType, new[] { "导入新Excel", "重新导入Excel", "移除Excel的导入", "EXCEL_SO文件回写Excel", "表格枚举" }, GUILayout.Height(50));

			switch (operType)
			{
				case ExcelOperateType.LoadData:
					RefreshLoadData();
					break;
				case ExcelOperateType.ReloadData:
					CommonAllOper(allOperates1, operates1);
					break;
				case ExcelOperateType.RemoveData:
					CommonAllOper(EditorUtil.RemoveAllExcelSO, "移除所有Excel的Asset文件，并删除导入记录!!!", operates2);
					break;
				case ExcelOperateType.WriteToExcel:
					CommonAllOper(EditorUtil.WriteAllExcel, "所有Asset文件写入Excel!!!", operates3);
					break;
				case ExcelOperateType.OtherOp:
					RefreshEnumOper();
					break;
			}
			#endregion
		}

		/// <summary>
		/// 通用操作面板
		/// </summary>
		/// <param name="allOper">所有选项操作回调</param>
		/// <param name="allOperText">所有选项操作按钮文本</param>
		/// <param name="operates">单条目所有的操作</param>
		public void CommonAllOper(Action allOper, string allOperText, List<ExcelFileOperate> operates)
		{
			if (GUILayout.Button(allOperText, GUILayout.Height(50)))
			{
				allOper();
			};
			GUILayout.BeginHorizontal();
			GUILayout.Space(10);
			scrollPos = GUILayout.BeginScrollView(scrollPos);
			ShowAllLoadedExcel(operates);
			GUILayout.EndScrollView();
			GUILayout.EndHorizontal();
		}

		/// <summary>
		/// 通用操作面板
		/// </summary>
		/// <param name="allOper">所有选项操作回调</param>
		/// <param name="allOperText">所有选项操作按钮文本</param>
		/// <param name="operates">单条目所有的操作</param>
		public void CommonAllOper(List<ExcelAllOperate> allOper, List<ExcelFileOperate> operates)
		{
			GUILayout.BeginHorizontal();
			foreach (var item in allOper)
			{
				if (GUILayout.Button(item.OperText, GUILayout.Height(50)))
				{
					item.Oper();
				};
			}
			GUILayout.EndHorizontal();
			GUILayout.BeginHorizontal();
			GUILayout.Space(10);
			scrollPos = GUILayout.BeginScrollView(scrollPos);
			ShowAllLoadedExcel(operates);
			GUILayout.EndScrollView();
			GUILayout.EndHorizontal();
		}

		public void ShowAllLoadedExcel(List<ExcelFileOperate> operates)
		{
			for(int i = ExcelNoteSO.Ins.notes.Count - 1; i >= 0; i--)
			{
				string excelPath = ExcelNoteSO.Ins.notes[i].excelPath;
				GUILayout.BeginHorizontal(GUILayout.MinHeight(40));
				GUILayout.BeginVertical(GUILayout.Width(500));
				GUILayout.Space(10);
				EditorGUILayout.LabelField("Excel表格资源路径:	" + excelPath);
				GUILayout.EndVertical();
				GUILayout.FlexibleSpace();
				foreach (var item in operates)
				{
					if (GUILayout.Button(item.btnText, GUILayout.Width(100), GUILayout.ExpandHeight(true)))
					{
						item.action(excelPath);
					};
				}
				GUILayout.Space(15);

				GUILayout.EndHorizontal();
				GUILayout.Box("", GUILayout.Height(2), GUILayout.ExpandWidth(true));
			}
		}

		/// <summary>
		/// 显示通用Excel导入格式要求
		/// </summary>
		public void RefreshCommonLoad(int index)
		{
			GUIStyle customLabelStyle = new GUIStyle(EditorStyles.label);
			customLabelStyle.normal.textColor = Color.red;
			GUILayout.Space(5);

			showContent = EditorGUILayout.Foldout(showContent, "Excel格式要求:\t");
			if (showContent)
			{
				EditorGUILayout.LabelField("	字段名称以[K]开头代表主键, 以[L]开头代表多语言字段", customLabelStyle);
				EditorGUILayout.LabelField("	字段valid、信息是否有效、int类型：1有效", customLabelStyle);
				EditorGUILayout.LabelField("	第一行设置字段的备注，阐述字段的作用等");
				EditorGUILayout.LabelField("	第二行设置字段名称：小写开头的驼峰命名法，例: systemId、name，fightValue等");
				EditorGUILayout.LabelField("	第三行设置字段的数据类型：例如基本数据：int，string，char，float，double，long，ulong，short，ushort。\n\t\t\t\t枚举格式：枚举名称，例如：DialoguePos", customLabelStyle, GUILayout.Height(40));
				EditorGUILayout.LabelField("	第一列设置当前行信息的备注");
			}


			GUILayout.Space(5);
			EditorGUILayout.LabelField("Excel资源路径案例:	" + @"Assets/FrameWork/ExcelTool/ExcelSample.xlsx");
			GUILayout.Box("", GUILayout.Height(5), GUILayout.ExpandWidth(true));
			EditorGUILayout.BeginHorizontal();
			excelPath = EditorGUILayout.TextField("Excel资源路径", excelPath);
			if (GUILayout.Button("选择表格", GUILayout.Width(80)))
			{
				string directory = GlobalUtil.GetAbsolutePath(PathSettingSO.Ins.excelDirectory);
				string path = EditorUtility.OpenFilePanel("选择Excel表格文件", directory, "xlsx");
				excelPath = GlobalUtil.GetAssetsRelativePath(path);
				if (index == 5) ExcelNoteSO.Ins.SaveEnumPath(excelPath);
				if (index == 1) ExcelNoteSO.Ins.SaveLastPath(excelPath);
			}
			EditorGUILayout.EndHorizontal();
			GUILayout.Space(10);
		}

		/// <summary>
		/// 显示Excel导入信息
		/// </summary>
		public void RefreshLoadData()
		{
			excelPath = ExcelNoteSO.Ins.lastExcelPath;
			RefreshCommonLoad(1);
			GUILayout.Space(10);
			if (ExcelNoteSO.Ins.CheckIfSetTableInfo(excelPath))
			{
				EditorGUILayout.LabelField("已生成并编译结构体");
			}
			if (ExcelNoteSO.Ins.CheckIfSetTableSO(excelPath))
			{
				EditorGUILayout.LabelField("导入Excel数据");
			}
			GUILayout.Space(10);
			GUILayout.BeginHorizontal();
			if (GUILayout.Button("生成并编译结构体", GUILayout.Height(50)))
			{
				EditorUtil.GenerateExcelStruct(excelPath);
			}
			if (GUILayout.Button("导入Excel数据", GUILayout.Height(50)))
			{
				ExcelUtil.LoadExcel(excelPath);
			}
			if (GUILayout.Button("生成并导入", GUILayout.Height(50)))
			{
				EditorUtil.NewStructAndLoadExcel(excelPath);
			}
			GUILayout.EndHorizontal();
		}

		/// <summary>
		/// 刷新其他杂项操作界面
		/// </summary>
		public void RefreshEnumOper()
		{
			excelPath = ExcelNoteSO.Ins.enumExcelPath;
			RefreshCommonLoad(5);
			GUILayout.Space(10);
			GUILayout.BeginHorizontal();
			if (GUILayout.Button("刷新Excel枚举", GUILayout.Height(50)))
			{
				EditorUtil.GenerateExcelEnum(excelPath);
			}
			GUILayout.EndHorizontal();
			GUILayout.Space(10);
			GUILayout.Box("", GUILayout.Height(5), GUILayout.ExpandWidth(true));
		}
	}

}
